package test.webmagic;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebElement;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.selenium.SeleniumDownloader;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import us.codecraft.webmagic.scheduler.RedisScheduler;
import us.codecraft.webmagic.selector.Selectable;

/**
 * 花瓣网抽取器。<br>
 * 使用Selenium做页面动态渲染。<br>
 */
public class SportGameProcessor implements PageProcessor {
    
    private Site site;
    
    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy /MM / dd");
    
    @Override
    public void process(Page page) {
        WebDriver driver = new ChromeDriver();
        driver.get(page.getUrl().get());
        
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        RemoteWebElement welElement = (RemoteWebElement)driver.findElement(By.id("chooseDate"));
        System.out.println("welElement: " + welElement.getClass());
        welElement.click();
        
        List<WebElement> dateOptions = welElement.findElements(By.xpath("//*/a[@onclick=\"ChooseDate(this)\"]"));
        System.out.println("dateOptions.size()： " + dateOptions.size());
//        for (WebElement webElement : dateOptions) {
//            System.out.println("text: " + webElement.getText());
//        }
        if (CollectionUtils.isNotEmpty(dateOptions)) {//*[@id="dateOption"]/a[1]
            dateOptions.get(7).click();
        }
        
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        WebElement dateElement = welElement.findElement(By.xpath("//*[@id='displayDate']"));
        String dateText = dateElement.getText();
        dateText = dateText.substring(0, dateText.lastIndexOf("("));
        System.out.println(dateText);
        Calendar calendar = GregorianCalendar.getInstance();
        dateText = calendar.get(Calendar.YEAR) + " /" + dateText;
        try {
            System.out.println(formatter.parse(dateText));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        List<WebElement> tableElements = welElement.findElements(By.xpath("//table[contains(@rel, 'bk_')]"));
        System.out.println("tableElements: " + tableElements.size());
        for (WebElement tableElement : tableElements) {
            
            WebElement captionElement = tableElement.findElement(By.xpath("./caption/a"));
            System.out.println("text: " + captionElement.getText());
            
            
            List<WebElement> mainRowElements = tableElement.findElements(By.xpath("./*//tr[contains(@class, 'mainRow')]"));
            System.out.println("game count: " + mainRowElements.size());
            
            for (WebElement mainRowElement : mainRowElements) {
                WebElement statusElement = mainRowElement.findElement(By.xpath("./td[contains(@class, 'status')]"));
                System.out.println("statusElement: " + statusElement.getText());
//                System.out.println(mainRowElement.getText());
                String id = mainRowElement.getAttribute("id");
                String prefix = id.substring(0, id.lastIndexOf("_"));
                
                WebElement team2TrElement = tableElement.findElement(By.xpath("./*//tr[contains(@id, '" + prefix+ "_secondRow')]"));
//                System.out.println(team2TrElement.getText());
                
                WebElement timeElement = mainRowElement.findElement(By.xpath("./td[contains(@class, 'time')]"));
                System.out.print(timeElement.getText() + " ");
                
                WebElement team1TdElement = mainRowElement.findElement(By.xpath("./td[contains(@class, 'teamName')]"));
                WebElement team2TdElement = team2TrElement.findElement(By.xpath("./td[contains(@class, 'teamName')]"));
                
                if (team1TdElement != null && team2TdElement != null) {
                    System.out.println(team1TdElement.getText() + " VS " + team2TdElement.getText());
                    List<WebElement> team1ScoresElements = mainRowElement.findElements(By.xpath("./td[contains(@class, 'score')]"));
                    for (WebElement team1ScoreElement : team1ScoresElements) {
                        String score  = team1ScoreElement.getText();
                        System.out.print(score + " ");
                    }
                    System.out.print(" : ");
                    List<WebElement> team2ScoresElements = team2TrElement.findElements(By.xpath("./td[contains(@class, 'score')]"));
                    for (WebElement team2ScoreElement : team2ScoresElements) {
                        String score  = team2ScoreElement.getText();
                        System.out.print(score + " ");
                    }
                    System.out.println();
                }
                
            }
        }
        
    }
    
    @Override
    public Site getSite() {
        if (site == null) {
            site = Site.me().setDomain("web1.sa8888.net").setSleepTime(1000);
        }
        return site;
    }
  //*[@id="waterfall"]/div[10]/a/img
    public static void main(String[] args) {
        System.setProperty("selenuim_config",
            "H:\\Work\\Git\\Mitdy-SportGame\\Mitdy-SportGame\\src\\main\\resources\\config.ini");
        Spider.create(new SportGameProcessor())
            .thread(2)
            .addUrl("http://web1.sa8888.net/?lang=2&ball=bk")
            .setScheduler(new QueueScheduler())
            .addPipeline(new FilePipeline("C:\\迅雷下载"))
            .setDownloader(new SeleniumDownloader("H:\\Work\\Git\\Mitdy-SportGame\\Mitdy-SportGame\\src\\main\\resources\\chromedriver.exe"))
            .run();
    }
}